package com.xuhuan.demo.digest;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Author huan.xu
 * @Date 2022/10/23 14:36
 * @Description base64加密解密工具类
 */
public class Base64Util {

    public Base64Util(){}

    final static Base64.Encoder encoder = Base64.getEncoder();
    final static Base64.Decoder decoder = Base64.getDecoder();

    /**
     * 加密
     * @param text
     * @return
     */
    public static String encode(String text) {
        return encoder.encodeToString(text.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 解密
     * @param encodedText
     * @return
     */
    public static String decode(String encodedText) {
        return new String(decoder.decode(encodedText), StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        String str = "1234qwer";

        System.out.println("编码后的字符串为：");
        System.out.println(Base64Util.encode(str));

        System.out.println("解码后的字符串为：");
        System.out.println(Base64Util.decode(Base64Util.encode(str)));
    }
}
